package com.example.mall.web.controller;

import com.example.mall.domain.SiteMessages;
import com.example.mall.dto.SiteMessagesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SiteMessagesQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ISiteMessagesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/siteMessages")
public class SiteMessagesController {

    @Autowired
    public ISiteMessagesService siteMessagesService;

    /**
     * 分页查询站内消息表信息
     * @param siteMessagesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询站内消息表信息",notes = "分页查询站内消息表信息",httpMethod = "GET")
    public JSONResult queryPage(SiteMessagesQuery siteMessagesQuery){
        PageDTO<SiteMessagesDto> siteMessagesDtoPageDTO = siteMessagesService.queryPage(siteMessagesQuery);
        return JSONResult.success("查询成功",siteMessagesDtoPageDTO);
    }

    /**
     * 根据id查询站内消息表信息
     * @param messageId
     * @return
     */
    @GetMapping("query/{messageId}")
    @ApiOperation(value = "根据id查询站内消息表信息",notes = "根据id查询站内消息表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("messageId")Long messageId){
        //1.根据id查询用户信息
        if (messageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.MESSAGEID_NOT_ID);
        }
        SiteMessages siteMessages = siteMessagesService.getById(messageId);
        return JSONResult.success("查询成功",siteMessages);
    }

    /**
     * 根据id查询站内消息表信息
     * @param messageId
     * @return
     */
    @GetMapping("querySiteMessagesById/{messageId}")
    @ApiOperation(value = "根据id查询站内消息表信息",notes = "根据id查询站内消息表信息",httpMethod = "GET")
    public JSONResult querySiteMessagesById(@PathVariable("messageId")Long messageId){
        //1.根据id查询用户信息
        if (messageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.MESSAGEID_NOT_ID);
        }
        SiteMessages siteMessages = siteMessagesService.querySiteMessagesById(messageId);
        return JSONResult.success("查询成功",siteMessages);
    }

    /**
     * 查询全部站内消息表信息
     * @param
     * @return
     */
    @GetMapping("querySiteMessagesList")
    @ApiOperation(value = "查询全部站内消息表信息",notes = "查询全部站内消息表信息",httpMethod = "GET")
    public JSONResult querySiteMessagesList(){
        List<SiteMessages> siteMessages = siteMessagesService.querySiteMessagesList();
        return JSONResult.success("查询成功",siteMessages);
    }

    /**
     * 新增站内消息表
     * @param siteMessagesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增站内消息表",notes = "新增站内消息表",httpMethod = "POST")
    public JSONResult insert(@RequestBody SiteMessagesDto siteMessagesDto){
        siteMessagesService.insert(siteMessagesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改站内消息表信息
     * @param siteMessagesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改站内消息表信息",notes = "修改站内消息表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody SiteMessagesDto siteMessagesDto){
        siteMessagesService.update(siteMessagesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除站内消息表信息
     * @param messageId
     * @return
     */
    @PostMapping("delete/{messageId}")
    @ApiOperation(value = "删除站内消息表信息",notes = "删除站内消息表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("messageId") Long messageId) {
        siteMessagesService.delete(messageId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除站内消息表信息
     * @param messageIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除站内消息表信息",notes = "批量删除站内消息表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> messageIds) {
        siteMessagesService.deleteBatch(messageIds);
        return JSONResult.success("批量删除成功");
    }
}
